package com.example.demo.demos.controller;

import com.example.demo.demos.service.ISmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.TimeUnit;


@RestController
@CrossOrigin
@Slf4j
public class SmsController {

    @Autowired
    private ISmsService smsService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @RequestMapping("/send-sms-code")
    public String sendCode(@RequestParam String phoneNumber) {
        // 生成随机验证码
        String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));

        // 将验证码发送给用户
        boolean success = smsService.sendVerificationCode(phoneNumber, code);

        if (success) {
            // 你可以将验证码存储在Redis中，并设置过期时间
            redisTemplate.opsForValue().set("SMS_" + phoneNumber, code, 5, TimeUnit.MINUTES);
            return "验证码发送成功";
        } else {
            return "验证码发送失败";
        }
    }
}